package com.java.shop.product.controller;

import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.shop.product.entity.AttrEntity;
import com.java.shop.product.entity.AttrGroupEntity;
import com.java.shop.product.service.AttrGroupService;
import com.java.shop.product.service.AttrService;
import com.java.shop.product.service.CategoryService;
import com.java.shop.product.vo.AttrGroupWithAttrVo;
import com.java.shop.product.vo.AttrRelationVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author cxq
 * @email 137512476@qq.com
 * @date 2021-03-26 11:19:52
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrService attrService;


    /**
     * 列表
     */
    @GetMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(name = "categoryId") Long categoryId){
//        PageUtils page =  attrGroupService.queryPage(params,categoryId);
        PageUtils page =  attrGroupService.myQueryPage(params,categoryId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @GetMapping("/mylist/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R mylist(@RequestParam Map<String, Object> params,@PathVariable(name = "categoryId") Long categoryId){
        PageUtils page =  attrGroupService.myQueryPage(params,categoryId);
        return R.ok().put("page", page);
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> attrList = attrGroupService.getRelationAttr(attrGroupId);
        return R.ok().put(attrList);
    }

    @ApiOperation("获取分类下所有分组&关联属性")
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrVo> data =  attrGroupService.getGroupWithAttr(catelogId);
        return R.ok().put(data);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        List<Long> parentIds = categoryService.getParentIds(attrGroup.getCatelogId());

        return R.ok().put("attrGroup", attrGroup).put("parents",parentIds);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateDetails(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 批量删除
     * @param attrRelationVo
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody List<AttrRelationVo> attrRelationVo){
        attrGroupService.deleteBatchRelation(attrRelationVo);
        return R.ok();
    }

    @GetMapping("/{groupId}/noattr/relation")
    public R noattrRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("groupId") Long groupId){
        PageUtils page = attrService.noAttrRelation(params,groupId);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrRelationVo> attrRelationVo){
        attrGroupService.saveBatchRelation(attrRelationVo);
        return R.ok();
    }
}
